package suncertify.view.server;

import suncertify.service.remote.Server;
import suncertify.service.remote.ServerAction;
import suncertify.view.AsyncActionListener;
import suncertify.util.Notification;
import suncertify.util.Messages;

import javax.swing.*;
import java.util.Observer;
import java.util.Observable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Implementation of server swing menu
 */
public class ServerMenu extends JMenu implements Observer {
    private static final Messages msg = Messages.getInstance();
    private JMenuItem startItem;
    private JMenuItem stopItem;
    private JMenuItem exitItem;
    private ServerOptionsDialog optDialog;
    private final Server server = Server.getIntance();

    /**
     * constructor
     * @param parent parent frame
     */
    public ServerMenu(JFrame parent) {
        super(msg.get("ui.server.menu.file"));
        this.optDialog = new ServerOptionsDialog(parent);
        startItem = new JMenuItem(msg.get("ui.server.menu.file.start"));
        startItem.addActionListener(new AsyncActionListener() {
            public void doAsync(ActionEvent event) {
                server.start();
            }
        });

        stopItem = new JMenuItem(msg.get("ui.server.menu.file.stop"));
        stopItem.addActionListener(new AsyncActionListener() {
            public void doAsync(ActionEvent event) {
                server.stop();
            }
        });

        JMenuItem optItem = new JMenuItem(msg.get("ui.server.menu.file.options"));
        optItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                optDialog.setVisible(true);
            }
        });

        stopItem.setEnabled(false);
        exitItem = new JMenuItem(msg.get("ui.server.menu.file.exit"));
        exitItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                server.shutdown();
                System.exit(0);
            }
        });

        add(startItem);
        add(stopItem);
        add(optItem);
        add(exitItem);

        server.addObserver(this);
    }

    /**
     * updates the current view componen state
     * @param observable event source
     * @param o event object sent to observers
     */
    public void update(Observable observable, final Object o) {
        if (o instanceof Notification) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {

                    Notification notification = (Notification) o;
                    switch ((ServerAction) notification.getEvent()) {
                        case RMI_CONNECT_ERROR:
                            startItem.setEnabled(true);
                            break;
                        case ERROR:
                            stopItem.setEnabled(server.isStarted());
                            startItem.setEnabled(!server.isStarted());
                            break;
                        case RMI_STARTED:
                        case STARTING:
                        case STOPING:
                            startItem.setEnabled(false);
                            stopItem.setEnabled(false);
                            break;
                        case STARTED:
                            stopItem.setEnabled(true);
                            break;
                        case STOPED:
                            startItem.setEnabled(true);
                            break;
                    }
                }
            });
        }
    }
}
